 <?php
/**
 * @version 1.0
 * @author jonathan <jo.link.noir@gmail.com>
 * @date 17-04-2010
 */
class U1_Removable_Resource extends SW_Removable_Group
{
	/**
	 * @param SW_Move $move
	 * @param SW_Units $units
	 */
	public function depose(SW_Move $move, SW_Units $units)
	{
		if (($planet = SW_Player::getInstancePlayerIfExists(U1_Planet::INDEX, $move->player_target))
		 && ($resource = $planet->getInstanceIfExists(U1_Resource::INDEX, $move->getEndPoint())))
			$resource->increase($this->quantities, $move->getTime());
		else
			U1_Resource_DAO::getFactory()->increaseResource($move->getEndPoint(), $this->quantities);
	}

	/**
	 * @param array $resources
	 * @param int $freight
	 * @return array
	 */
	public function getQuantitiesAndFilter(array $resources, $freight)
	{
		foreach ($this->quantities as $id => &$v)
		{
			$min = ($resources[$id] > $freight) ? $freight : $resources[$id];
			if ($min < $v)
				$v = $min;
			$freight -= $v;
		}
		SF_Process_Array::removeEmptyValue($this->quantities);
		return $this->quantities;
	}
}